unit CreateTable;

{
  PasCalc
  Version 1.2 2011-05-26

  Copyright (C) 2011 KEEPER <yar.keeper@gmail.com>

  This file is part of the PasCalc Calculator

  This unit is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  See the file LICENSE.TXT, included in this distribution,
  for details about the copyright.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, Buttons;

type

  { TfrmCreateTable }

  TfrmCreateTable = class(TForm)
    btnAddPoint: TBitBtn;
    btnChange: TBitBtn;
    btnDelPoint: TBitBtn;
    btnChangePoint: TBitBtn;
    btnPlot: TBitBtn;
    btnOpen: TBitBtn;
    edX: TEdit;
    edY: TEdit;
    lblX: TLabel;
    lblY: TLabel;
    lb: TListBox;
    dlgOpen: TOpenDialog;
    procedure btnChangeClick(Sender: TObject);
    procedure btnAddPointClick(Sender: TObject);
    procedure btnChangePointClick(Sender: TObject);
    procedure btnDelPointClick(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
    procedure btnPlotClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure lbClick(Sender: TObject);
  private
    fEdError: string;
    function GetArg(const idx: integer): extended;
    function GetValue(const idx: integer): extended;
  public
    fBuilded: boolean;
    procedure LangInit(str: TStringList);
  end; 

var
  frmCreateTable: TfrmCreateTable;

implementation

uses
  Figure;

{ TfrmCreateTable }

procedure TfrmCreateTable.btnAddPointClick(Sender: TObject);
begin
  try
    StrToFloat(edX.Text);
    StrToFloat(edY.Text);
  except
    ShowMessage(fEdError);
  end;
  lb.Items.Add(edX.Text + ' ' + edY.Text);
end;

procedure TfrmCreateTable.btnChangeClick(Sender: TObject);
begin
  lb.Items.Delete(lb.ItemIndex);
  btnAddPointClick(self);
  btnChange.Visible := false;
  lb.ItemIndex := 0;
end;

procedure TfrmCreateTable.btnChangePointClick(Sender: TObject);
begin
  btnChange.Visible := true;
  edX.Text := FloatToStr(getArg(lb.ItemIndex));
  edY.Text := FloatToStr(getValue(lb.ItemIndex));
end;

procedure TfrmCreateTable.btnDelPointClick(Sender: TObject);
begin
  lb.Items.Delete(lb.ItemIndex);
  btnDelPoint.Enabled := lb.Items.Count <> 0;
end;

procedure TfrmCreateTable.btnOpenClick(Sender: TObject);
begin
  if dlgOpen.Execute then
    lb.Items.LoadFromFile(UTF8toANSI(dlgOpen.FileName));
end;

procedure TfrmCreateTable.btnPlotClick(Sender: TObject);
var
  i: integer;
begin
  if lb.Items.Count = 0 then exit;
  SetLength(frmGraph.FuncValues[0], lb.Items.Count);
  SetLength(frmGraph.FuncValues[1], lb.Items.Count);
  for i := 0 to high(frmGraph.FuncValues[0]) do
  begin
    frmGraph.FuncValues[0, i] := GetArg(i);
    frmGraph.FuncValues[1, i] := GetValue(i);
  end;
  fBuilded := true;
  ModalResult := 2;
end;

procedure TfrmCreateTable.FormShow(Sender: TObject);
begin
  fBuilded := false;
end;

procedure TfrmCreateTable.lbClick(Sender: TObject);
begin
  btnDelPoint.Enabled := true;
  btnChangePoint.Enabled := true;
end;

procedure TfrmCreateTable.LangInit(str: TStringList);
begin
  btnAddPoint.Caption := str[90];
  btnDelPoint.Caption := str[91];
  btnOpen.Caption := str[92];
  btnPlot.Caption := str[93];
  fEdError := str[94];
  Caption := str[95];
end;

function TfrmCreateTable.GetArg(const idx: integer): extended;
var
  SpacePos: integer;
begin
  SpacePos := pos(' ', lb.Items.Strings[idx]);
  result := StrToFloat(copy(lb.Items.Strings[idx], 1, SpacePos - 1));
end;

function TfrmCreateTable.GetValue(const idx: integer): extended;
var
  SpacePos: integer;
begin
  SpacePos := pos(' ', lb.Items.Strings[idx]);
  result := StrToFloat(copy(lb.Items.Strings[idx], SpacePos + 1,
    Length(lb.Items.Strings[idx]) - SpacePos));
end;

initialization
  {$I CreateTable.lrs}

end.

